package com.springboot.manager.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.manager.common.CommonResult;
import com.springboot.manager.entity.User;
import com.springboot.manager.enums.ResultCodeEnum;
import com.springboot.manager.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理
 *
 * @author zhangzl
 * @since 2021-08-23
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    /**
     * 用户-新增
     *
     * @param user
     * @return
     */
    @PostMapping(value = "/add")
    public CommonResult add(@RequestBody @Validated User user) {
        CommonResult<Object> commonResult = new CommonResult<Object>(ResultCodeEnum.FAILURE.getCode(), user);
        try {
            if (ObjectUtil.isNotNull(user)) {
                boolean hasSave = userService.save(user);
                if (hasSave) {
                    log.info("用户新增成功");
                    commonResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                } else {
                    commonResult.setMessage("用户新增失败");
                }
            } else {
                commonResult.setMessage("用户新增失败-入参为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            commonResult.setCode(ResultCodeEnum.EXCEPTION.getCode());
            commonResult.setMessage("用户新增异常");
        }
        return commonResult;
    }

    /**
     * 用户-删除
     *
     * @param id
     * @return
     */
    @PostMapping(value = "/remove")
    public CommonResult remove(String id) {
        int i = 1 / 0;
        CommonResult<Object> commonResult = new CommonResult<>(ResultCodeEnum.FAILURE.getCode(), id);
        try {
            if (StrUtil.isNotBlank(id)) {
                Boolean remove = userService.removeById(id);
                if (remove) {
                    commonResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                } else {
                    commonResult.setMessage("用户删除失败");
                }
            } else {
                commonResult.setMessage("用户删除失败-入参为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            commonResult.setCode(ResultCodeEnum.EXCEPTION.getCode());
            commonResult.setMessage("用户删除异常");
        }
        return commonResult;
    }


    /**
     * 用户-列表
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping(value = "/list")
    public CommonResult list(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                             @RequestParam(name = "name", required = false) String name
    ) {
        CommonResult<Object> commonResult = new CommonResult<>(ResultCodeEnum.FAILURE.getCode());
        try {
            Page<User> userPage = new Page<>(pageNo, pageSize);
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("is_del", "0");
            if (StrUtil.isNotBlank(name)) {
                queryWrapper.like("name", name);
            }

            IPage<User> userInfoPage = userService.page(userPage, queryWrapper);
            if (ObjectUtil.isNotEmpty(userInfoPage)) {
                commonResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                commonResult.setData(userInfoPage);
            } else {
                commonResult.setCode(ResultCodeEnum.FAILURE.getCode());
                commonResult.setMessage("用户列表查询失败");
            }
        } catch (Exception e) {
            commonResult.setCode(ResultCodeEnum.EXCEPTION.getCode());
            commonResult.setMessage("用户列表查询异常");
        }
        return commonResult;
    }


}
